<?php
/**
 * ClassName: member_info
 * Description: 用户信息控制器
 * @Author 周昌
 * @Create 2023/12/5 17:30
 */

namespace app;


use epii\server\Args;
use model\MemberModel;
use service\admin\ArchivesService;
use service\admin\MemberInfoService;
use service\admin\HospitalService;
use service\admin\MemberService;

class member_info extends base
{
    protected $admin_allow=['index','ajax_data','add_edit','del','getFamily'];
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }

    public function index()
    {
        $member_id=Args::params('member_id/d');
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        $this->_as_member_id=$member_id;
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        //用户列表下 家庭组成员模块专用
        $member_id=Args::params('member_id/d');
        if($member_id){
            $map[]=['member_id','=',$member_id];
        }
        //筛选
        $name = Args::params('name');
        if ($name) {
            $map[] = ['name', "LIKE", '%' . $name . '%'];
        }
        $phone=Args::params('phone');
        if($phone){
            $member_id=MemberModel::getValue(['phone'=>$phone],'id');
            if($member_id){
                $map[]=['member_id','=',$member_id];
            }
        }
        echo MemberInfoService::getAjaxData($map);
    }

    public function add_edit()
    {
        $id = Args::params('id');
        $member_id = Args::params('member_id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            //验证
            $this->validate($data,[
//                'phone|手机号'=>'require|mobile',
                'name|姓名'=>'require|chsAlphaNum',
                'sex|性别'=>'require|integer',
                'relation|关系'=>'require|integer',
//                'family_group|归属家庭组'=>'require|max:20',
                'birth_date|阳历生日'=>'max:20',
                'lunar_birth|阴历生日'=>'max:20',
                'nation|民族'=>'chs',
                'height|身高'=>'float',
                'weight|体重'=>'float',
                'address|家庭地址'=>'max:30',
                'education|学历'=>'integer',
                'work|工作性质'=>'max:50',
                'family_rank|家庭排行'=>'max:10',
                'marriage_childbirth|婚育情况'=>'max:30',
//                'hospital_id|所属医院'=>'require|integer|>=:0',
            ]);
            $data['member_id']=$member_id;
            if ($id) {
                $res = MemberInfoService::edit($id,$data);
            } else {
                $data['addtime']=time();
                $res=MemberInfoService::add($data);
            }
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info = $id ? MemberInfoService::getMemberInfo($id) : [];
        $this->_as_id = $id;
        $this->_as_member_id = $member_id;
        $this->_as_relation = ArchivesService::archivesRelationSelect(1);
        $this->_as_education = ArchivesService::archivesEducationSelect();
        $this->_as_sex = MemberService::memberSexSelect();

        $this->adminUiDisplay();
    }

    //家庭成员列表独立专用
    public function add_edit1()
    {
        $id = Args::params('id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            //验证
            $this->validate($data,[
                'phone|手机号'=>'require|mobile',
                'name|姓名'=>'require|chsAlphaNum',
                'sex|性别'=>'require|integer',
                'relation|关系'=>'require|integer',
                'family_group|归属家庭组'=>'require|max:20',
                'birth_date|阳历生日'=>'max:20',
                'lunar_birth|阴历生日'=>'max:20',
                'nation|民族'=>'chs',
                'height|身高'=>'float',
                'weight|体重'=>'float',
                'address|家庭地址'=>'max:30',
                'education|学历'=>'integer',
                'work|工作性质'=>'max:50',
                'family_rank|家庭排行'=>'max:10',
                'marriage_childbirth|婚育情况'=>'max:30',
//                'hospital_id|所属医院'=>'require|integer|>=:0',
            ]);
            //判断用户是否已经存在
            $res=MemberService::memberIsExist($id,$data['phone'],$data['hospital_id']);
            if($res['code']){
                $this->error('手机号尚未注册请先去注册用户');
            }
            unset($data['family_group']);
            unset($data['phone']);
            $data['member_id']=$res['data']['id'];
            if ($id) {
                $res = MemberInfoService::edit($id,$data);
            } else {
                $data['addtime']=time();
                $res=MemberInfoService::add($data);
            }
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info = $id ? MemberInfoService::getMemberInfo($id) : [];
        $this->_as_id = $id;
        $this->_as_relation = ArchivesService::archivesRelationSelect(1);
        $this->_as_education = ArchivesService::archivesEducationSelect();
        $this->_as_sex = MemberService::memberSexSelect();

        $this->adminUiDisplay();
    }

    /**
     * @return array|false|string* 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断用户下是否已有会员卡，无则可以删除
        $res = MemberInfoService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }
    //获取家庭组
    public function getFamily()
    {
        $phone=Args::params('phone/1','手机号不能为空');
        $res=MemberService::getFamilyGroup(['phone'=>$phone]);
        exit(json_encode($res));
    }

}